// 兼容IE9及以下的 classlist,DOMTokenList[待补] 不存在问题
if (!("classList" in document.documentElement)) {
    Object.defineProperty(HTMLElement.prototype, 'classList', {
        get: function() {
            var self = this;

            function update(fn) {
                return function(value) {
                    var classes = self.className.split(/\s+/g),
                        index = classes.indexOf(value);

                    fn(classes, index, value);
                    self.className = classes.join(" ");
                }
            }

            return {
                add: update(function(classes, index, value) {
                    if (!~index) classes.push(value);
                }),

                remove: update(function(classes, index) {
                    if (~index) classes.splice(index, 1);
                }),

                toggle: update(function(classes, index, value) {
                    if (~index)
                        classes.splice(index, 1);
                    else
                        classes.push(value);
                }),

                contains: function(value) {
                    return !!~self.className.split(/\s+/g).indexOf(value);
                },

                item: function(i) {
                    return self.className.split(/\s+/g)[i] || null;
                }
            };
        }
    });
}

if (!("DOMTokenList" in window)) {

    window.DOMTokenList = function(ids) {
        'use strict';
        var idsArray = [],
            self = this,
            parse = function(id, functionName, cb) {
                var search = id.toString();
                if (search.split(' ').length > 1) {
                    throw new Error("Failed to execute '" + functionName + "' on 'TokenList': The token provided ('" + search + "') contains HTML space characters, which are not valid in tokens.');");
                } else {
                    cb(search);
                }
            };

        function triggerAttributeChange() {
            if (self.tokenChanged && typeof self.tokenChanged === 'function') {
                self.tokenChanged(idsArray.toString());
            }
        }

        if (ids && typeof ids === 'string') {
            idsArray = ids.split(' ');
        }
        self.item = function(index) {
            return idsArray[index];
        };

        self.contains = function(id) {
            parse(id, 'contains', function(search) {
                return idsArray.indexOf(search) !== -1;
            });
        };

        self.add = function(id) {
            parse(id, 'add', function(search) {
                if (idsArray.indexOf(search) === -1) {
                    idsArray.push(search);
                }
                triggerAttributeChange();
            });
        };

        self.remove = function(id) {
            parse(id, 'remove', function(search) {
                idsArray = idsArray.filter(function(item) {
                    return item !== id;
                });
                triggerAttributeChange();
            });
        };

        self.toggle = function(id) {
            parse(id, 'toggle', function(search) {
                if (!self.contains(search)) {
                    self.add(search);
                } else {
                    self.remove(search);
                }
            });
        };

        self.tokenChanged = null;

        self.toString = function() {
            var tokens = '',
                i;
            if (idsArray.length > 0) {
                for (i = 0; i < idsArray.length; i = i + 1) {
                    tokens = tokens + idsArray[i] + ' ';
                }
                tokens = tokens.slice(0, tokens.length - 1);
            }
            return tokens;
        };
    };

}




var util = {
    checkBrowser: function() {
        var _userAgent = navigator.userAgent;

        // Opera 8.0+
        var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

        // Firefox 1.0+
        var isFirefox = typeof InstallTrigger !== 'undefined';

        // Safari 3.0+ "[object HTMLElementConstructor]"
        var isSafari = /constructor/i.test(window.HTMLElement) || (function(p) {
            return p.toString() === '[object SafariRemoteNotification]';
        }(!window.safari || safari.pushNotification));

        // Internet Explorer 6-11
        var getIeVersion = (function() {

            var userAgent = navigator.userAgent.toLowerCase(),
                uaMatch;
            if (userAgent.match(/msie ([\d.]+)/) != null) {
                //ie6--ie9
                uaMatch = userAgent.match(/msie ([\d.]+)/);
                return /9\.0|10\.0/gi.test(uaMatch);
            } else if (userAgent.match(/(trident)\/([\w.]+)/)) {
                uaMatch = userAgent.match(/trident\/([\w.]+)/);
                return /6\.0|7\.0/gi.test(uaMatch);
                // switch (uaMatch[1]) {
                //     case "4.0":
                //         return "IE8";
                //         break;
                //     case "5.0":
                //         return "IE9";
                //         break;
                //     case "6.0":
                //         return "IE10";
                //         break;
                //     case "7.0":
                //         return "IE11";
                //         break;
                //     default:
                //         return "undefined";
                // }
            }

        })();


        var isIE = /* @cc_on!@*/ false || !!document.documentMode;
        var isIE11 = getIeVersion;
        // Edge 20+
        var isEdge = !isIE && !!window.StyleMedia;

        // Chrome 1+
        var isChrome = !!window.chrome && !!window.chrome.csi && !!window.chrome.loadTimes;

        // Blink engine detection
        var isBlink = (isChrome || isOpera) && !!window.CSS;

        // isSougou
        var isSougou = /metasr/gi.test(window.navigator.userAgent);
        // alert(getIeVersion)
        return isChrome || isFirefox || isSafari || isSougou || isEdge || isOpera || isIE11;
    },
    isMSESupported: (function() {
        var mediaSource = typeof window !== 'undefined' ? window.MediaSource || window.WebKitMediaSource : null;
        var sourceBuffer = window.SourceBuffer || window.WebKitSourceBuffer;
        var isTypeSupported = mediaSource && typeof mediaSource.isTypeSupported === 'function' && mediaSource.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"');
        var sourceBufferValidAPI = !sourceBuffer ||
            (sourceBuffer.prototype &&
                typeof sourceBuffer.prototype.appendBuffer === 'function' &&
                typeof sourceBuffer.prototype.remove === 'function');
        return !!isTypeSupported && !!sourceBufferValidAPI;
    })(),
    supportsNativeHls: (function(){//移动设备对m3u8原生解析是否支持
        var a = document.createElement("video");
        var b = ["application/vnd.apple.mpegurl", "audio/mpegurl", "audio/x-mpegurl", "application/x-mpegurl", "video/x-mpegurl", "video/mpegurl", "application/mpegurl"];
        return b.some(function(b) {
            return /maybe|probably/i.test(a.canPlayType(b))
        });
    })(),
    isMobile: (function() {
        var sUserAgent = navigator.userAgent.toLowerCase(),
            // bIsIpad = sUserAgent.match(/ipad/i) ? true : false,
            bIsIphoneOs = sUserAgent.match(/iphone os/i) ? true : false,
            bIsMidp = sUserAgent.match(/midp/i) ? true : false,
            bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) ? true : false,
            bIsUc = sUserAgent.match(/ucweb/i) ? true : false,
            bIsAndroid = sUserAgent.match(/android/i) ? true : false,
            bIsCE = sUserAgent.match(/windows ce/i) ? true : false,
            bIsWM = sUserAgent.match(/windows mobile/i) ? true : false,
            bIsWebview = sUserAgent.match(/webview/i) ? true : false;
        return (bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM);
    })(),
    isWeiXin: (function(){
        var ua = window.navigator.userAgent.toLowerCase();
        //通过正则表达式匹配ua中是否含有MicroMessenger字符串
        if(ua.match(/MicroMessenger/i) == 'micromessenger'){
            return true;
        }else{
            return false;
        }
    })(),
    isApp:(function(){
        var ua = window.navigator.userAgent.toLowerCase();
        //通过正则表达式匹配ua中是否含有FlutterApp字符串
        if(ua.match(/FlutterApp/i) == 'flutterapp'){
            return true;
        }else{
            return false;
        }
    })(),
    checkBrowserResult: function() {
        document.write('<div class="alert" style="width:50%; text-align: center; top: 32%; position: absolute; left: 25%; padding: 40px;">为了更好地系统体验，建议您使用最新 谷歌/火狐/Safari/Edge/Opera 浏览器。</div>');
    },
    vueConfig: function(vue) {

        if (process.env.NODE_ENV == 'production') {
            // console.log('----',process.env.NODE_ENV,'----');
            vue.config.silent = true;
            vue.config.devtools = false;
            vue.config.productionTip = false;
        }
    }
};

// var _location = window.location;
// if(util.isMobile) {
//     if (/^\/$/gi.test(_location.pathname) && /^#\/(home)?$/gi.test(_location.hash)) {
//         window.location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.xuetangx.mobile.xuetangxcloud";
//     }
// }

export default util;
